﻿#include "FrozenMiner.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/Data/Stat.h"
#include "../MapObject.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    FrozenMiner::FrozenMiner(MonsterInfo *info) : MonsterObject(info)
    {
    }

    void FrozenMiner::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        MirDirection dir = Functions::PreviousDir(getDirection());
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        std::vector<MapObject*> targets = FindAllTargets(1, getCurrentLocation());

        if ((targets.size() > 1 && getEnvir()->Random->Next(2) == 0) || getEnvir()->Random->Next(8) == 0)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 1;
            Broadcast(tempVar);

            for (int i = 0; i < targets.size(); i++)
            {
                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 1000, {targets[i], static_cast<int>(damage * 0.8), DefenceType::ACAgility});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectAttack *tempVar2 = new S::ObjectAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->Type = 0;
            Broadcast(tempVar2);
            DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 600, {getTarget(), damage, DefenceType::ACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }

        ShockTime = 0;
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        if (getTarget()->Dead)
        {
            FindTarget();
        }
    }
}
